package com.gold.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gold.entity.Card;
import com.gold.entity.Mobile;
import com.gold.entity.Prepaid;
import com.gold.entity.User;
import com.gold.mapper.CardMapper;
import com.gold.mapper.MobileMapper;
import com.gold.mapper.PrepaidMapper;
import com.gold.mapper.UserMapper;
import com.gold.myexception.Myexception;
import com.gold.service.UserService;
import com.gold.util.Myutil;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper um;
	@Autowired
	private CardMapper cm;
	@Autowired
	private MobileMapper mm;
	@Autowired
	private PrepaidMapper pm;

	public List<User> selectAllUser() {
		return um.selectAllUser();
	}

	public List<User> selectUserByLikeAndPage(Map<String, String> map) {
		if (map.get("userName") == null) {
			map.put("userName", "");
		}
		if (map.get("userPhone") == null) {
			map.put("userPhone", "");
		}
		int pageSize = 10;
		int userCount = um.selectUserByLike(map).size();
		int pageCount = userCount % pageSize == 0 ? userCount / pageSize : userCount / pageSize + 1;
		String nowPageString = map.get("nowPageString");
		if (nowPageString == null) {
			nowPageString = "1";
		}
		int nowPage = Integer.parseInt(nowPageString);
		if (nowPage <= 0) {
			nowPage = pageCount;
		}
		if (nowPage > pageCount) {
			nowPage = 1;
		}
		map.put("start", "" + ((nowPage - 1) * pageSize + 1));
		map.put("end", "" + (nowPage * pageSize));
		map.put("nowPageString", nowPage + "");
		return um.selectUserByLikeAndPage(map);
	}

	public List<User> selectUserByLike(Map<String, String> map) {
		return um.selectUserByLike(map);
	}

	public List<User> selectUserByPage(Map<String, String> map) {
		int pageSize = 2;
		int userCount = um.selectAllUser().size();
		int pageCount = userCount % pageSize == 0 ? userCount / pageSize : userCount / pageSize + 1;
		String nowPageString = map.get("nowPageString");
		if (nowPageString == null) {
			nowPageString = "1";
		}
		int nowPage = Integer.parseInt(nowPageString);
		if (nowPage <= 0) {
			nowPage = pageCount;
		}
		if (nowPage > pageCount) {
			nowPage = 1;
		}
		map.put("start", "" + ((nowPage - 1) * pageSize + 1));
		map.put("end", "" + (nowPage * pageSize));
		map.put("nowPageString", nowPage + "");
		return um.selectUserByPage(map);
	}

	public void updateUserStatusIdByUserId(String[] arr, String status) {
		for (int i = 0; i < arr.length; i++) {
			User user = new User(Integer.parseInt(arr[i]), "", "", null, "", "", 0, 0, 0, Integer.parseInt(status), "", null);
			um.updateUserStatusIdByUserId(user);
		}
	}

	public User selectUserByUserName(String userName) {
		return um.selectUserByUserName(userName);
	}

	public User userLogin(String userName, String userPwd) throws Myexception {
		User user = um.selectUserByUserName(userName);
		if (user == null) {
			throw new Myexception("用户不存在");
		}
		if (user.getUserStatusId() == 2) {
			throw new Myexception("用户已经被禁用");
		}
		if (!userPwd.equals(user.getUserPwd())) {
			throw new Myexception("密码错误");
		}
		return user;
	}

	public void insertUser(User user) throws Myexception {
		User oldUser = um.selectUserByUserName(user.getUserName());
		if (oldUser != null) {
			throw new Myexception("用户名已经存在");
		}
		um.insertUser(user);
	}

	public void updateUserPwdByUserId(String userName, String userPwd, String userPwd2, String userPhone) throws Myexception {
		User user = um.selectUserByUserName(userName);
		if (user == null) {
			throw new Myexception("用户名不存在");
		}
		if (!userPwd.equals(userPwd2)) {
			throw new Myexception("密码不一致");
		}
		if (!userPhone.equals(user.getUserPhone())) {
			throw new Myexception("手机号码输入错误");
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("userPwd", userPwd);
		map.put("userId", user.getUserId() + "");
		um.updateUserPwdByUserId(map);
	}

	public void updateUserCurrencyByUserId(Map<String, String> map) throws Myexception {

	}

	public void updateUserTariffeByUserId(Map<String, String> map) throws Myexception {
		// TODO Auto-generated method stub

	}

	public User selectUserByUserId(int userId) {
		return um.selectUserByUserId(userId);
	}

	public void updateUserCardAmountByUserId(Map<String, String> map) throws Myexception {
		String userIdString = map.get("userIdString");
		if (Myutil.checkNumberIsNumber(userIdString)) {
			int userId = Integer.parseInt(userIdString);
			User user = um.selectUserByUserId(userId);
			if (user != null) {
				String cardNumber = map.get("cardNumber");
				Card card = cm.selectCardByCardNumber(cardNumber);
				if (card != null) {
					if (card.getCardStatusId() == 2) {
						int MobileNumber = Integer.parseInt(user.getUserPhone().substring(0, 2));
						Mobile mobile = mm.selectMobileByMobileNumber(MobileNumber);
						if (mobile != null) {
							if (mobile.getMobileArea().equals(card.getCardProv()) || card.getCardProvId() == 0) {
								String cardPwd = map.get("cardPwd");
								if (card.getCardPwd().equals(cardPwd)) {
									cm.updateCardStatusByCardId(card.getCardId());
									Map<String, String> newMap = new HashMap<String, String>();
									newMap.put("userCardAmount", user.getUserCardAmount() + card.getCardAmount() + "");
									newMap.put("userId", user.getUserId() + "");
									um.updateUserCardAmountByUserId(newMap);
									pm.insertPrepaid(new Prepaid(0, user.getUserId(), card.getCardId(), null));
								} else {
									throw new Myexception("密保卡密码错误");
								}
							} else {
								throw new Myexception("手机号与充值卡省份不匹配");
							}
						} else {
							throw new Myexception("数据库没有该手机区域信息");
						}
					} else {
						throw new Myexception("密保卡不可用");
					}
				} else {
					throw new Myexception("卡号不存在");
				}
			} else {
				throw new Myexception("未知错误");
			}
		} else {
			throw new Myexception("未知错误");
		}
	}

}
